#########################################################################################
####################################  FIGURE A3   #######################################
###################  Study 1 Team-Building Task Measures Over Time  #####################
#########################################################################################

library(ggplot2)
library(haven)
library(DescTools)
library(readxl)
library(plyr)
library(dplyr)
library(gridExtra)
library(ggpubr)
library(rstatix)
library(tidyverse)

##Change working directory
#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_a3")

#########################################################################################


lab_time_b5 <- read.csv("intermediate_data/genderminfl_bylab_forgraphing.csv")
#lab_time_b5 <- read.csv("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/output/intermediate/genderminfl_bylab_forgraphing.csv")
lab_time_b5$time <- factor(lab_time_b5$time, levels = c("Lab 1", "Lab 2"), labels = c("Task 1", "Task 2"))
lab_time_b5 <- mutate(lab_time_b5, condition = replace(condition, condition == "1F Group", "Majority Male"))
lab_time_b5 <- mutate(lab_time_b5, condition = replace(condition, condition == "3F Group", "Majority Female"))
#lab_time_b5 <- mutate(lab_time_b5, condition = ifelse(condition == "1F Group", "Majority Male", "Majority Female"))

#spokes_time_b5 <- read.csv("genderspokes_bylab_forgraphing.csv")
spokes_time_b5 <- read.csv("intermediate_data/genderspokes_bylab_forgraphing.csv")
spokes_time_b5$time <- factor(spokes_time_b5$time, levels = c("Lab 1", "Lab 2"), labels = c("Task 1", "Task 2"))
spokes_time_b5 <- mutate(spokes_time_b5, condition = replace(condition, condition == "1F Group", "Majority Male"))
spokes_time_b5 <- mutate(spokes_time_b5, condition = replace(condition, condition == "3F Group", "Majority Female"))
#lab_time_b5 <- mutate(lab_time_b5, condition = ifelse(condition == "1F Group", "Majority Male", "Majority Female"))


png(filename = "figure_a3a.png", width = 600, height = 400)
fig_time2_b5 <- ggplot(data=lab_time_b5, aes(x=time, y=estimate, group=condition, colour=condition)) + 
  expand_limits(y=c(0,1.3)) + 
  geom_line(size = 1.25, stat="identity", position=position_dodge(0.15)) + 
  geom_point(size = 4, stat="identity", position=position_dodge(0.15)) + 
  xlab("Time") + ylab("Ratio of votes for women \n to group gender composition") + 
  theme(plot.title = element_text(hjust=.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour="black"), legend.key = element_rect(fill=NA)) + 
  geom_hline(yintercept = 1, linetype="dashed") + 
  scale_colour_manual(name='', breaks = c("Majority Male", "Majority Female"), values=c('Majority Male' = 'dodgerblue3', 'Majority Female' = 'darkgoldenrod2')) +
  scale_y_continuous(breaks = seq(0, 1, by = 0.25))
fig_time2_b5
dev.off()

png(filename = "figure_a3b.png", width = 600, height = 400)
fig_time3_b5 <- ggplot(data=spokes_time_b5, aes(x=time, y=estimate, group=condition, colour=condition)) + 
  expand_limits(y=c(0,1.3)) + 
  geom_line(size = 1.25, stat="identity", position=position_dodge(0.15)) + 
  geom_point(size = 4, stat="identity", position=position_dodge(0.15)) + 
  xlab("Time") + ylab("Ratio of votes for women \n to group gender composition") + 
  theme(plot.title = element_text(hjust=.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour="black"), legend.key = element_rect(fill=NA)) + 
  geom_hline(yintercept = 1, linetype="dashed") + 
  scale_colour_manual(name='', breaks = c("Majority Male", "Majority Female"), values=c('Majority Male' = 'dodgerblue3', 'Majority Female' = 'darkgoldenrod2')) +
  scale_y_continuous(breaks = seq(0, 1, by = 0.25))
fig_time3_b5
dev.off()

